package com.example.menuvirtualclient.adapter;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.example.menuvirtualclient.FullscreenActivity;
import com.example.menuvirtualclient.R;
import com.example.menuvirtualclient.core.Dish;
import com.example.menuvirtualclient.core.Menu;
import com.example.menuvirtualclient.core.OrderDetail;
import com.example.menuvirtualclient.core.Restaurant;
import com.example.menuvirtualclient.presenter.MenuPresenter;
import com.example.menuvirtualclient.rule.OrderDetailRule;
import com.example.menuvirtualclient.view.IView;

public class OrderAdapter extends ArrayAdapter<OrderDetail> implements IView {
	Context context;
	int layoutResourceId;
	OrderDetail objects[] = null;

	private OrderDetailRule _orderDetailRule;
	// private OrderRule _orderRule;
	// private DishRule _dishRule;

	public OrderAdapter(Context context, int layoutResourceId,
			OrderDetail[] objects) {
		super(context, layoutResourceId, objects);
		// TODO Auto-generated constructor stub
		this.context = context;
		this.layoutResourceId = layoutResourceId;
		this.objects = objects;
		// _orderDetailRule = new OrderDetailRule(context);
		// _orderRule = new OrderRule(context);
		// _dishRule = new DishRule(context);
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		View row = convertView;
		ComponentsHolder holder;
		final int cc = position;

		// row = null; // This clears the view, so the rows are well drawn but
		// makes the app much slower

		if (row == null) {
			LayoutInflater inflater = ((Activity) context).getLayoutInflater();
			row = inflater.inflate(layoutResourceId, parent, false);

			holder = new ComponentsHolder();
			holder.idMenu = (TextView) row.findViewById(R.id.idMenu);
			holder.idDish = (TextView) row.findViewById(R.id.idDish);
			holder.name = (TextView) row.findViewById(R.id.name);
			holder.price = (TextView) row.findViewById(R.id.price);
			holder.quantity = (TextView) row.findViewById(R.id.quantity);
			holder.remove = (Button) row.findViewById(R.id.remove);
			holder.spin = (Spinner) row.findViewById(R.id.punto);
			holder.addComments = (Button) row.findViewById(R.id.addComments);

			row.setTag(holder);
		} else {
			holder = (ComponentsHolder) row.getTag();

			holder.idMenu.setText("");
			holder.idDish.setText("");
			holder.name.setText("");
			holder.price.setText("");
			holder.quantity.setText("");
		}

		OrderDetail componentsList = objects[cc];
		// EN REALIDAD GRABO EL ID DEL ORDER_DETAIL
		holder.idMenu.setText((String.valueOf(componentsList.getId())));
		// -----------------------------------------------------------------
		holder.idDish
				.setText((String.valueOf(componentsList.getDish().getId())));
		holder.name.setText(componentsList.getDish().getName());
		// holder.quantity.setText(String.valueOf(componentsList.getQuantity()));
		holder.price.setText(String
				.valueOf(componentsList.getDish().getPrice()));
//		holder.remove.setOnClickListener(new View.OnClickListener() {
//			
//			@Override
//			public void onClick(View v) {
//				try {
//					TextView tvIdDetail = (TextView) ((RelativeLayout) v
//							.getParent().getParent()).findViewById(R.id.idMenu);
//					int idDetail = Integer.valueOf(tvIdDetail.getText()
//							.toString());
//					_orderDetailRule = new OrderDetailRule(v.getContext());
//					_orderDetailRule.deleteOrderDetail(new OrderDetail(idDetail));
//				} catch (Exception e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
//				
//			}
//		});

		if (componentsList.getDish().getCookingPoint() != 0) {

			String[] array = context.getResources().getStringArray(
					R.array.puntos);
			ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(
					context, R.layout.layout_spinner_puntos, array);
			holder.spin.setAdapter(spinnerArrayAdapter);

			holder.spin.setOnItemSelectedListener(new OnItemSelectedListener() {

				@Override
				public void onItemSelected(AdapterView<?> arg0, View arg1,
						int arg2, long arg3) {

					MenuPresenter presenter = new MenuPresenter(
							OrderAdapter.this);

					TextView tvIdDetail = (TextView) ((RelativeLayout) arg1
							.getParent().getParent()).findViewById(R.id.idMenu);
					int idDetail = Integer.valueOf(tvIdDetail.getText()
							.toString());

					for (OrderDetail od : FullscreenActivity
							.getOrderDetailsList()) {

						if (od.getDish().getCookingPoint() != 0
								&& od.getId() == idDetail) {
							od.setCookingPoint(arg2);
							presenter.UpdateOrderDetail(idDetail, arg2);

						}

					}
					// TODO Auto-generated method stub
					// ((TextView)
					// arg0.getChildAt(0)).setBackgroundColor(Color.WHITE);
				}

				@Override
				public void onNothingSelected(AdapterView<?> arg0) {
					// TODO Auto-generated method stub

				}
			});
		} else {

			holder.spin.setVisibility(holder.spin.GONE);
		}
		
		if (componentsList.getStatus()==2){
			holder.remove.setVisibility(holder.remove.GONE);
			holder.addComments.setVisibility(holder.addComments.GONE);
			
		}
		
		return row;
	}

	private void ShowToast(String message) {
		Toast toast = Toast.makeText(context, message, Toast.LENGTH_SHORT);
		toast.show();
	}

	static class ComponentsHolder {
		TextView idMenu;
		TextView idDish;
		TextView name;
		TextView price;
		TextView quantity;
		Button remove;
		Spinner spin;
		Button addComments;
	}

	@Override
	public void SetMessage(String title, String message) {
		// TODO Auto-generated method stub

	}

	@Override
	public void LoadEntityList(List<Dish> dishes) {
		// TODO Auto-generated method stub

	}

	@Override
	public Context GetContext() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void GetOrder(String table, Restaurant restaurant) {
		// TODO Auto-generated method stub

	}

	@Override
	public void GetOrderDetails(List<OrderDetail> orderDetails) {
		// TODO Auto-generated method stub

	}

	@Override
	public void LoadMainMenu(Menu[] menues) {
		// TODO Auto-generated method stub

	}

	@Override
	public void ShowEnquiry() {
		// TODO Auto-generated method stub

	}

	@Override
	public void ShowTables() {
		// TODO Auto-generated method stub

	}

	@Override
	public void CloseView(View v) {
		// TODO Auto-generated method stub

	}

	@Override
	public void LoadRestaurant(Restaurant restaurant) {
		// TODO Auto-generated method stub

	}

	@Override
	public void navigate(Class<?> destination) {
		// TODO Auto-generated method stub

	}

	@Override
	public void navigate(Class<?> destination, boolean logoutFlag) {
		// TODO Auto-generated method stub

	}

	@Override
	public void navigate(Class<?> destination, Bundle extras) {
		// TODO Auto-generated method stub

	}

	@Override
	public void showError(String errorMessage) {
		// TODO Auto-generated method stub

	}

}
